pacman::p_load(foreign, dplyr, tidyr, tibble, xtable)

precinct.num <- function(df) {
  vec1<-c()
  vec2<-c()
  frac <- df%>%
    dplyr::select(is.numeric)
  for (z in 1:ncol(frac)){
    vec2<-append(as.numeric(apply(frac[,z]%>%
                                    as.data.frame(.)%>%
                                    na.omit(.),2,length)),vec2)
  }
  for (i in 1:(ncol(frac)-1)){
    for (j in (i+1):ncol(frac)){
      vec1<-append((sum(complete.cases(frac[,i], frac[,j]))),vec1)
      print(sum(complete.cases(frac[,i], frac[,j])))
    }
  }
  return(list(max.complete=max(vec1),
              min.complete=min(vec1),
              min.obs=min(vec2),
              max.obs=max(vec2)))
}#extract number of 1) complete observations across candidates and 2) number of non-NA values for each candidate
setwd("")#set working directory

ca20<-read.csv("state_p20_sov_data_by_p20_svprec.csv")
ca16<-read.csv("state_p16_sov_data_by_p16_svprec/state_p16_sov_data_by_p16_svprec.csv")
ca12 <- read.csv("state_p12_sov_data_by_p12_svprec/state_p12_sov_data_by_p12_svprec.csv")
ca08 <- read.dbf("state_s08_sov_data_by_s08_svprec/state_s08_sov_data_by_s08_svprec.dbf", as.is = T)

#2020 ----
ca20.1 <- ca20 %>%
  filter(!grepl("TOT|SOV", SVPREC_KEY)) %>%
  dplyr::select(SVPREC_KEY, contains("PRS")) %>%
  mutate(SVPREC_KEY = gsub("A", "", SVPREC_KEY)) %>%
  mutate(across(-SVPREC_KEY, as.numeric)) %>%
  group_by(SVPREC_KEY) %>% 
  dplyr::summarize(across(everything(), ~ sum(. , na.rm = TRUE)))

#clean key
key.20 <- read.csv("https://statewidedatabase.org/pub/data/D20/SOV/codebooks/p20_sov_codebook.csv") %>%
  mutate(CONTEST = gsub("_P20", "", CONTEST),
         NAME = gsub(" ", "", NAME),    
         NAME = gsub("\\.", "", NAME),  
         NAME = tolower(NAME)) %>%
  filter(grepl("PRS", CONTEST)) %>%
  dplyr::select(CONTEST, NAME) %>%
  mutate(PARTY = gsub("PRS([A-Za-z]+)\\d*", "\\1", CONTEST))

#add identifiers to colnames
for (i in 1:ncol(ca20.1)){
  if (colnames(ca20.1)[i] %in% key.20$CONTEST){
    colnames(ca20.1)[i] <- paste0(key.20$PARTY[key.20$CONTEST %in% colnames(ca20.1)[i]],"_", key.20$NAME[key.20$CONTEST %in% colnames(ca20.1)[i]],"_20")
  }
}

#dems
ca20.1.dems <- ca20.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("DEM"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div"))

#reps
ca20.1.reps <- ca20.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP")) %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("REP"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div")) 

colnames(ca20.1.dems) <- tolower(colnames(ca20.1.dems))
colnames(ca20.1.reps) <- tolower(colnames(ca20.1.reps))

#2016 ----- 
ca16.1 <- ca16 %>%
  filter(!grepl("TOT|SOV", SVPREC_KEY)) %>%
  dplyr::select(SVPREC_KEY, contains("PRS")) %>%
  mutate(SVPREC_KEY = gsub("A", "", SVPREC_KEY)) %>%
  mutate(across(-SVPREC_KEY, as.numeric)) %>%
  group_by(SVPREC_KEY) %>% 
  dplyr::summarize(across(everything(), ~ sum(. , na.rm = TRUE)))

#clean key
key.16 <- read.csv("https://statewidedatabase.org/pub/data/D20/SOV/codebooks/p16_sov_codebook.csv") %>%
  mutate(CONTEST = gsub("_P16", "", CONTEST),
         NAME = gsub(" ", "", NAME),    
         NAME = gsub("\\.", "", NAME),  
         NAME = tolower(NAME)) %>%
  filter(grepl("PRS", CONTEST)) %>%
  dplyr::select(CONTEST, NAME) %>%
  mutate(PARTY = gsub("PRS([A-Za-z]+)\\d*", "\\1", CONTEST))

#add identifiers to colnames
for (i in 1:ncol(ca16.1)){
  if (colnames(ca16.1)[i] %in% key.16$CONTEST){
    colnames(ca16.1)[i] <- paste0(key.16$PARTY[key.16$CONTEST %in% colnames(ca16.1)[i]],"_", key.16$NAME[key.16$CONTEST %in% colnames(ca16.1)[i]],"_16")
  }
}

#dems
ca16.1.dems <- ca16.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("DEM"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div"))

#reps
ca16.1.reps <- ca16.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP")) %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("REP"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div")) 

colnames(ca16.1.dems) <- tolower(colnames(ca16.1.dems))
colnames(ca16.1.reps) <- tolower(colnames(ca16.1.reps))

# 2012----- 
ca12.1 <- ca12 %>%
  filter(!grepl("TOT|SOV", SVPREC_KEY)) %>%
  dplyr::select(SVPREC_KEY, contains("PRS")) %>%
  mutate(SVPREC_KEY = gsub("A", "", SVPREC_KEY)) %>%
  mutate(across(-SVPREC_KEY, as.numeric)) %>%
  group_by(SVPREC_KEY) %>% 
  dplyr::summarize(across(everything(), ~ sum(. , na.rm = TRUE)))

#clean key
key.12 <- read.csv("https://statewidedatabase.org/pub/data/D20/SOV/codebooks/p12_sov_codebook.csv") %>%
  mutate(CONTEST = gsub("_P12", "", CONTEST),
         NAME = gsub(" ", "", NAME),    
         NAME = gsub("\\.", "", NAME),  
         NAME = tolower(NAME)) %>%
  filter(grepl("PRS", CONTEST)) %>%
  dplyr::select(CONTEST, NAME) %>%
  mutate(PARTY = gsub("PRS([A-Za-z]+)\\d*", "\\1", CONTEST))

#add identifiers to colnames
for (i in 1:ncol(ca12.1)){
  if (colnames(ca12.1)[i] %in% key.12$CONTEST){
    colnames(ca12.1)[i] <- paste0(key.12$PARTY[key.12$CONTEST %in% colnames(ca12.1)[i]],"_", key.12$NAME[key.12$CONTEST %in% colnames(ca12.1)[i]],"_12")
  }
}

#dems
ca12.1.dems <- ca12.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("DEM"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div")) 

#reps
ca12.1.reps <- ca12.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP")) %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("REP"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div")) 

colnames(ca12.1.dems) <- tolower(colnames(ca12.1.dems))
colnames(ca12.1.reps) <- tolower(colnames(ca12.1.reps))


# 2008 ----- 
ca08.1 <- ca08 %>%
  filter(!grepl("TOT|SOV", SVPREC_KEY)) %>%
  dplyr::select(SVPREC_KEY, contains("PRS")) %>%
  mutate(SVPREC_KEY = gsub("A", "", SVPREC_KEY)) %>%
  mutate(across(-SVPREC_KEY, as.numeric)) %>%
  group_by(SVPREC_KEY) %>% 
  dplyr::summarize(across(everything(), ~ sum(. , na.rm = TRUE)))

#clean key
key.08 <- read.csv("https://statewidedatabase.org/pub/data/D20/SOV/codebooks/s08_sov_codebook.csv") %>%
  mutate(CONTEST = gsub("_S08", "", CONTEST),
         NAME = gsub(" ", "", NAME),    
         NAME = gsub("\\.", "", NAME),  
         NAME = tolower(NAME)) %>%
  filter(grepl("PRS", CONTEST)) %>%
  dplyr::select(CONTEST, NAME) %>%
  mutate(PARTY = gsub("PRS([A-Za-z]+)\\d*", "\\1", CONTEST))

#add identifiers to colnames
for (i in 1:ncol(ca08.1)){
  if (colnames(ca08.1)[i] %in% key.08$CONTEST){
    colnames(ca08.1)[i] <- paste0(key.08$PARTY[key.08$CONTEST %in% colnames(ca08.1)[i]],"_", key.08$NAME[key.08$CONTEST %in% colnames(ca08.1)[i]],"_08")
  }
}

#dems
ca08.1.dems <- ca08.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("DEM"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div"))

#reps
ca08.1.reps <- ca08.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP")) %>%
  mutate(tot.votes = rowSums(across(-SVPREC_KEY), na.rm = TRUE)) %>%  
  mutate(across(contains("REP"), ~ . / tot.votes, .names = "div_{.col}")) %>%
  dplyr::select(SVPREC_KEY, contains("div")) 

colnames(ca08.1.dems) <- tolower(colnames(ca08.1.dems))
colnames(ca08.1.reps) <- tolower(colnames(ca08.1.reps))

#merge parties separately
reps.08.12.16.20 <- ca08.1.reps %>%
  full_join(ca12.1.reps, by="svprec_key") %>% 
  full_join(ca16.1.reps, by="svprec_key") %>% 
  dplyr::select(-svprec_key) %>% 
  dplyr::select(div_rep_mikehuckabee_08, div_rep_mittromney_08, 
                div_rep_johnmccain_08, div_rep_mittromney_12, 
                div_rep_ronpaul_12, div_rep_donaldtrump_16, 
                div_rep_johnrkasich_16)

dems.08.16.20 <- ca08.1.dems %>%
  full_join(ca16.1.dems, by="svprec_key") %>% 
  full_join(ca20.1.dems, by="svprec_key") %>%
  dplyr::select(-svprec_key)  %>%
  dplyr::select(div_dem_hillaryclinton_08, div_dem_barackobama_08, 
                div_dem_hillaryclinton_16,div_dem_berniesanders_16,
                div_dem_elizabethwarren_20, div_dem_berniesanders_20, 
                div_dem_michaelrbloomberg_20, div_dem_josephrbiden_20)

#prepare appendix table (dems)
cormatr <- cor(dems.08.16.20,use="pairwise.complete.obs")
cormatr2 <- cormatr[,-c(1,2)]

precinct.num(dems.08.16.20)

#NOTE: surprisingly low number of matches between 2008 and 2020 dems
test<-dems.08.16.20 <- ca08.1.dems %>%
  full_join(ca16.1.dems, by="svprec_key") %>% 
  full_join(ca20.1.dems, by="svprec_key") %>% 
  dplyr::select(svprec_key, div_dem_hillaryclinton_08, div_dem_elizabethwarren_20)

print(xtable(cormatr2, 
             label="t:ca:dems",
             caption=c("Correlations of vote shares in Democratic primaries from all California precincts (n=5705-26210)."),
             digits=c(0,rep(3,6))),
      file=c("tableA26.tex"))#table A26

tableA26 <- readLines("tableA26.tex")
tableA26 <- gsub("div\\\\_dem\\\\_hillaryclinton\\\\_08", "Clinton '08", tableA26, ignore.case = TRUE)
tableA26 <- gsub("div\\\\_dem\\\\_barackobama\\\\_08", "Obama '08", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_hillaryclinton\\\\_16", "Clinton '16", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_berniesanders\\\\_16", "Sanders '16", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_elizabethwarren\\\\_20", "Warren '20", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_berniesanders\\\\_20", "Sanders '20", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_michaelrbloomberg\\\\_20", "Bloomberg '20", tableA26)
tableA26 <- gsub("div\\\\_dem\\\\_josephrbiden\\\\_20", "Biden '20", tableA26)
writeLines(tableA26, "tableA26.tex")

#prepare corrs table for regression analysis 
cormatd3 <- data.frame(cbind(variable_2 = rownames(cormatr2), cormatr2))
cormatd4 <- reshape2::melt(cormatd3, id = c("variable_2"))

#extract years as a separate column
cormatd4$year_cand1_1 <- ifelse(grepl("08", cormatd4$variable_2), 2008, 0)
cormatd4$year_cand1_2 <- ifelse(grepl("16", cormatd4$variable_2), 2016, 0)
cormatd4$year_cand1_3 <- ifelse(grepl("20", cormatd4$variable_2), 2020, 0)
cormatd4$year_cand1_4 <- ifelse(grepl("12", cormatd4$variable_2), 2012, 0)
cormatd4$year_cand1 <- cormatd4$year_cand1_1 + cormatd4$year_cand1_2 + cormatd4$year_cand1_3 + cormatd4$year_cand1_4 
cormatd4$year_cand2_1 <- ifelse(grepl("08", cormatd4$variable), 2008, 0)
cormatd4$year_cand2_2 <- ifelse(grepl("16", cormatd4$variable), 2016, 0)
cormatd4$year_cand2_3 <- ifelse(grepl("20", cormatd4$variable), 2020, 0)
cormatd4$year_cand2_4 <- ifelse(grepl("12", cormatd4$variable), 2012, 0)
cormatd4$year_cand2 <- cormatd4$year_cand2_1 + cormatd4$year_cand2_2 + cormatd4$year_cand2_3 + cormatd4$year_cand2_4 
cormatd5 <-cormatd4 %>%
  subset(., select=c(variable_2, year_cand1, variable, year_cand2, value))
cormatd5$value <- as.numeric(cormatd5$value)
cormatd5$variable <- as.character(cormatd5$variable)
cormatd5$party <- "DEMS"
cormatd5$state <- 'CA'
write.csv(cormatd5, "ca_dems_groupbyyear_corrs.csv")

dems.grouped<-cormatd5%>%
  filter(year_cand1!=year_cand2)%>%
  filter(variable_2!=variable)%>%
  group_by(party,year_cand1,year_cand2)%>%
  dplyr::summarise(cor_mean=mean(value,na.rm=T), 
                   cor_median=median(value,na.rm=T))

#prepare appendix table (reps)
cormatr <- cor(reps.08.12.16.20,use="pairwise.complete.obs")
cormatr2 <- cormatr[,-c(1,2)]
print(xtable(cormatr2, digits=c(0,3,3,3,3,3))) #table A31

precinct.num(reps.08.12.16.20)

print(xtable(cormatr2,
             label="t:ca:gop",
             caption=c("Correlations of vote shares in Republican primaries from all California precincts (n=11989-26049)."),
             digits=c(0,rep(3,5))),
      file=c("tableA27.tex"))#table A27

tableA27 <- readLines("tableA27.tex")
tableA27 <- gsub("div\\\\_rep\\\\_mikehuckabee\\\\_08", "Huckabee '08", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_mittromney\\\\_08", "Romney '08", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_johnmccain\\\\_08", "McCain '08", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_mittromney\\\\_12", "Romney '12", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_ronpaul\\\\_12", "Paul '12", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_donaldtrump\\\\_16", "Trump '16", tableA27, ignore.case = TRUE)
tableA27 <- gsub("div\\\\_rep\\\\_johnrkasich\\\\_16", "Kasich '16", tableA27, ignore.case = TRUE)
writeLines(tableA27, "tableA27.tex")

#prepare corrs table for regression analysis 
cormatd3 <- data.frame(cbind(variable_2 = rownames(cormatr2), cormatr2))
cormatd4 <- reshape2::melt(cormatd3, id = c("variable_2"))

#extract years as a separate column
cormatd4$year_cand1_1 <- ifelse(grepl("08", cormatd4$variable_2), 2008, 0)
cormatd4$year_cand1_2 <- ifelse(grepl("16", cormatd4$variable_2), 2016, 0)
cormatd4$year_cand1_3 <- ifelse(grepl("20", cormatd4$variable_2), 2020, 0)
cormatd4$year_cand1_4 <- ifelse(grepl("12", cormatd4$variable_2), 2012, 0)
cormatd4$year_cand1 <- cormatd4$year_cand1_1 + cormatd4$year_cand1_2 + cormatd4$year_cand1_3 + cormatd4$year_cand1_4 
cormatd4$year_cand2_1 <- ifelse(grepl("08", cormatd4$variable), 2008, 0)
cormatd4$year_cand2_2 <- ifelse(grepl("16", cormatd4$variable), 2016, 0)
cormatd4$year_cand2_3 <- ifelse(grepl("20", cormatd4$variable), 2020, 0)
cormatd4$year_cand2_4 <- ifelse(grepl("12", cormatd4$variable), 2012, 0)
cormatd4$year_cand2 <- cormatd4$year_cand2_1 + cormatd4$year_cand2_2 + cormatd4$year_cand2_3 + cormatd4$year_cand2_4 
cormatd5 <-cormatd4 %>%
  subset(., select=c(variable_2, year_cand1, variable, year_cand2, value))
cormatd5$value <- as.numeric(cormatd5$value)
cormatd5$variable <- as.character(cormatd5$variable)
cormatd5$party <- "GOP"
cormatd5$state <- 'CA'
write.csv(cormatd5, "ca_gop_groupbyyear_corrs.csv")

gop.grouped<-cormatd5%>%
  filter(year_cand1!=year_cand2)%>%
  filter(variable_2!=variable)%>%
  group_by(party,year_cand1,year_cand2)%>%
  dplyr::summarise(cor_mean=mean(value,na.rm=T), 
                   cor_median=median(value,na.rm=T))

#prepare corr summary table
corr.summary<-rbind(dems.grouped, gop.grouped)%>%
  setNames(c("Party","Year 1", "Year 2", "Corr. Mean", "Corr. Median"))%>%
  mutate(Party=Party%>%dplyr::recode(
    "GOP"="Republicans",
    "DEMS"="Democrats"
  ))

# voteshares ------ 

#dem, 2008
ca08.dems.tot <- ca08.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY,  ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2008)
ca08.dems.tot$voteshare <- ca08.dems.tot$V1/sum(ca08.dems.tot$V1)
ca08.dems.tot <- data.frame(RowNames = rownames(ca08.dems.tot), ca08.dems.tot, row.names = NULL)

#rep, 2008
ca08.reps.tot <- ca08.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2008)
ca08.reps.tot$voteshare <- ca08.reps.tot$V1/sum(ca08.reps.tot$V1)
ca08.reps.tot <- data.frame(RowNames = rownames(ca08.reps.tot), ca08.reps.tot, row.names = NULL)

#dem, 2012
ca12.dems.tot <- ca12.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2012)
ca12.dems.tot$voteshare <- ca12.dems.tot$V1/sum(ca12.dems.tot$V1)
ca12.dems.tot <- data.frame(RowNames = rownames(ca12.dems.tot), ca12.dems.tot, row.names = NULL)

#rep, 2012
ca12.reps.tot <- ca12.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2012)
ca12.reps.tot$voteshare <- ca12.reps.tot$V1/sum(ca12.reps.tot$V1)
ca12.reps.tot <- data.frame(RowNames = rownames(ca12.reps.tot), ca12.reps.tot, row.names = NULL)

#dem, 2016
ca12.reps.tot
ca16.dems.tot <- ca16.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2016)
ca16.dems.tot$voteshare <- ca16.dems.tot$V1/sum(ca16.dems.tot$V1)
ca16.dems.tot <- data.frame(RowNames = rownames(ca16.dems.tot), ca16.dems.tot, row.names = NULL)

#rep, 2016
ca16.reps.tot <- ca16.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2016)
ca16.reps.tot$voteshare <- ca16.reps.tot$V1/sum(ca16.reps.tot$V1)
ca16.reps.tot <- data.frame(RowNames = rownames(ca16.reps.tot), ca16.reps.tot, row.names = NULL)


#dem, 2020
ca20.dems.tot <- ca20.1 %>%
  dplyr::select(SVPREC_KEY, contains("DEM"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2020)
ca20.dems.tot$voteshare <- ca20.dems.tot$V1/sum(ca20.dems.tot$V1)
ca20.dems.tot <- data.frame(RowNames = rownames(ca20.dems.tot), ca20.dems.tot, row.names = NULL)

#rep, 2020
ca20.reps.tot <- ca20.1 %>%
  dplyr::select(SVPREC_KEY, contains("REP"))  %>%
  mutate(CONS=1) %>% 
  group_by(CONS) %>%
  dplyr::summarise(across(-SVPREC_KEY, ~sum(. , na.rm = TRUE))) %>%
  t(.) %>%
  as.data.frame(.) %>%
  slice(-1) %>% 
  mutate(year =  2020)
ca20.reps.tot$voteshare <- ca20.reps.tot$V1/sum(ca20.reps.tot$V1)
ca20.reps.tot <- data.frame(RowNames = rownames(ca20.reps.tot), ca20.reps.tot, row.names = NULL)

voteshares <- rbind(ca20.reps.tot, ca20.dems.tot,
                    ca16.reps.tot, ca16.dems.tot,
                    ca12.reps.tot, ca12.dems.tot,
                    ca08.reps.tot, ca08.dems.tot)
write.csv(voteshares, "ca-voteshares.csv")

